home *** CD-ROM | disk | FTP | other *** search
/ Aminet 12 / Aminet 12 (1996)(GTI - Schatztruhe)[!][Jun 1996].iso / Aminet / misc / edu / calc_trainer.lha / Calctrainer / Source / Rechentrainer.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-05  |  20.4 KB  |  528 lines

  1. //#define WBWINNAME "con:0/10/320/120/Rechentrainer/CLOSE"
  2.  
  3. /*  Ansi  */
  4. #pragma +
  5. #include <math.h>
  6. #include <stdlib.h>
  7. #pragma -
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include <wbstartup.h>
  12.  
  13. /* Libraries */
  14. #include <libraries/mui.h>
  15. #include <libraries/gadtools.h>
  16.  
  17. /* protos */
  18. #include <clib/muimaster_protos.h>
  19. #include <clib/alib_protos.h>
  20. #include <clib/dos_protos.h>
  21. #include <clib/exec_protos.h>
  22.  
  23. /*  Pragmas  */
  24. #include <pragma/muimaster_lib.h>
  25. #include <pragma/exec_lib.h>
  26. #include <pragma/intuition_lib.h>
  27.  
  28.  
  29. void init(void);
  30. void end(void);
  31. int Multiplikation(char*);
  32. int Division(char*);
  33. int Addition(char*);
  34. int Subtraktion(char*);
  35. extern "C++" void Dateilesen(int[], int[]);
  36. extern "C++" void Dateischreiben(int[], int[]);
  37. void Zufall(int*, int*, int, int);
  38. void Vergleichen(int*, int*, int*);
  39. void auswertung(int[], int[], int*, int*, int*, int*, int*, int*, int*);
  40.  
  41.  
  42. /* MUIBuilder */
  43.  
  44. #include "rechentrainerGui.c"
  45.  
  46.  
  47. struct Library * MUIMasterBase;
  48. struct Library * LocaleBase;
  49.  
  50.  
  51. /* main function */
  52. int main(int argc, char *argv[])
  53. {
  54.         struct ObjApp * App = NULL;     /* Application object */
  55.         BOOL    running = TRUE;
  56.         ULONG   signal;
  57.         char *erga,*mal,str[12],z[8],x[8];
  58.         int j,i=0,erg,richtig[5]={0,0,0,0,0},falsch[5]={0,0,0,0,0};
  59.         int prozg=0, prozm=0, prozd=0, proza=0, prozs=0, gesr=0, gesf=0;
  60.  
  61.                           //zu oben [0]Mul, [1]Div, [2]Addi, [3]Subtr
  62.         init();           //Initialisierung
  63.  
  64.                           //Create Application von Mui-Builder
  65.         if (!(App = CreateApp()))
  66.         {
  67.                 printf("Can't Create App\n");
  68.                 end();
  69.         }
  70.         while (running)
  71.         {
  72.                 switch (DoMethod(App->App,MUIM_Application_Input,&signal))
  73.                 {
  74.                 case MUIV_Application_ReturnID_Quit:
  75.                         running = FALSE;
  76.                     break;
  77.                 case BT_AUSWAHL:
  78.                       auswertung(richtig,falsch,&prozg,&prozm,&prozd,&proza,&prozs,&gesr,&gesf);
  79.                       set(App->Gauge_Mul, MUIA_Gauge_Current, prozm);
  80.                       set(App->Gauge_Div, MUIA_Gauge_Current, prozd);
  81.                       set(App->Gauge_Addi, MUIA_Gauge_Current, proza);
  82.                       set(App->Gauge_Subtr, MUIA_Gauge_Current, prozs);
  83.                       set(App->Gauge_Ges, MUIA_Gauge_Current, prozg);
  84.                       inttostr(gesr,x);
  85.                       set(App->Gesammt_Richtig, MUIA_Text_Contents, x);
  86.                       inttostr(gesf,x);
  87.                       set(App->Gesammt_Falsch, MUIA_Text_Contents, x);
  88.                       inttostr(richtig[0],x);
  89.                       set(App->Mul_Richtig, MUIA_Text_Contents, x);
  90.                       inttostr(falsch[0],x);
  91.                       set(App->Mul_Falsch, MUIA_Text_Contents, x);
  92.                       inttostr(richtig[1],x);
  93.                       set(App->Div_Richtig, MUIA_Text_Contents, x);
  94.                       inttostr(falsch[1],x);
  95.                       set(App->Div_Falsch, MUIA_Text_Contents, x);
  96.                       inttostr(richtig[2],x);
  97.                       set(App->Addi_Richtig, MUIA_Text_Contents, x);
  98.                       inttostr(falsch[2],x);
  99.                       set(App->Addi_Falsch, MUIA_Text_Contents, x);
  100.                       inttostr(richtig[3],x);
  101.                       set(App->Subtr_Richtig, MUIA_Text_Contents, x);
  102.                       inttostr(falsch[3],x);
  103.                       set(App->Subtr_Falsch, MUIA_Text_Contents, x);
  104.                     break;
  105.                 case ANZ_MUL:
  106.                     get(App->Anz_Mul, MUIA_String_Contents, &mal);
  107.                     
  108.                     if (atoi(mal) != 0 )
  109.                        {
  110.                         erg=Multiplikation(str);
  111.                         set(App->Aufg_Mul, MUIA_Text_Contents, str);
  112.                         set(App->Mul_Nr, MUIA_Text_Contents, "1");
  113.                         i=atoi(mal);
  114.                         j=1;
  115.                        }
  116.                     else
  117.                        {
  118.                         set(App->Aufg_Mul, MUIA_Text_Contents,GetMBString(MSG_Rubish));
  119.                         set(App->Mul_Erg_tex, MUIA_Text_Contents,GetMBString(MSG_Rubish));
  120.                        }
  121.                     break;
  122.                 case ERG_MUL:
  123.                     get(App->Erg_Mul, MUIA_String_Contents, &erga);
  124.                     set(App->Mul_Erg, MUIA_Text_Contents, "");
  125.                     if(i)
  126.                      {
  127.                       ++j;
  128.                       if (erg == atoi(erga))
  129.                         {
  130.                            set(App->Mul_Erg_tex, MUIA_Text_Contents,GetMBString(MSG_Right));
  131.                            ++richtig[0];
  132.                            inttostr(richtig[0],x);
  133.                            set(App->Mul_Richtig, MUIA_Text_Contents, x);
  134.                         }
  135.                       else
  136.                         {
  137.                            ++falsch[0];
  138.                            inttostr(falsch[0],x);
  139.                            set(App->Mul_Falsch, MUIA_Text_Contents, x);
  140.                            set(App->Mul_Erg_tex, MUIA_Text_Contents,GetMBString(MSG_Wrong));
  141.                            inttostr(erg,x);
  142.                            set(App->Mul_Erg, MUIA_Text_Contents, x);
  143.                         }
  144.                       auswertung(richtig,falsch,&prozg,&prozm,&prozd,&proza,&prozs,&gesr,&gesf);
  145.                       set(App->Gauge_Mul, MUIA_Gauge_Current, prozm);
  146.                       set(App->Gauge_Div, MUIA_Gauge_Current, prozd);
  147.                       set(App->Gauge_Addi, MUIA_Gauge_Current, proza);
  148.                       set(App->Gauge_Subtr, MUIA_Gauge_Current, prozs);
  149.                       set(App->Gauge_Ges, MUIA_Gauge_Current, prozg);
  150.                       inttostr(gesr,x);
  151.                       set(App->Gesammt_Richtig, MUIA_Text_Contents, x);
  152.                       inttostr(gesf,x);
  153.                       set(App->Gesammt_Falsch, MUIA_Text_Contents, x);
  154.                       --i;
  155.                       if(i)
  156.                         {
  157.                            set(App->Erg_Mul, MUIA_String_Contents, "");
  158.                            inttostr(j,z);
  159.                            set(App->Mul_Nr, MUIA_Text_Contents, z);
  160.                            erg=Multiplikation(str);
  161.                            set(App->Aufg_Mul, MUIA_Text_Contents, str);
  162.                         }
  163.                       else
  164.                         {
  165.                            set(App->Aufg_Mul, MUIA_Text_Contents,GetMBString(MSG_Ready));
  166.                            set(App->Erg_Mul, MUIA_Text_Contents, "");
  167.                         }
  168.                      }
  169.                     break;
  170.                 case ANZ_DIV:
  171.                     get(App->Anz_Div, MUIA_String_Contents, &mal);
  172.                     
  173.                     if (atoi(mal) != 0 )
  174.                        {
  175.                         erg=Division(str);
  176.                         set(App->Aufg_Div, MUIA_Text_Contents, str);
  177.                         set(App->Div_Nr, MUIA_Text_Contents, "1");
  178.                         i=atoi(mal);
  179.                         j=1;
  180.                        }
  181.                     else
  182.                        {
  183.                         set(App->Aufg_Div, MUIA_Text_Contents,GetMBString(MSG_Rubish));
  184.                         set(App->Div_Erg_tex, MUIA_Text_Contents,GetMBString(MSG_Rubish));
  185.                        }
  186.                     break;
  187.                 case ERG_DIV:
  188.                     get(App->Erg_Div, MUIA_String_Contents, &erga);
  189.                     set(App->Div_Erg, MUIA_Text_Contents, "");
  190.                     if(i)
  191.                      {
  192.                       ++j;
  193.                       if (erg == atoi(erga))
  194.                         {
  195.                            set(App->Div_Erg_tex, MUIA_Text_Contents,GetMBString(MSG_Right));
  196.                            ++richtig[1];
  197.                            inttostr(richtig[1],x);
  198.                            set(App->Div_Richtig, MUIA_Text_Contents, x);
  199.                         }
  200.                       else
  201.                         {
  202.                            ++falsch[1];
  203.                            inttostr(falsch[1],x);
  204.                            set(App->Div_Falsch, MUIA_Text_Contents, x);
  205.                            set(App->Div_Erg_tex, MUIA_Text_Contents,GetMBString(MSG_Wrong));
  206.                            inttostr(erg,x);
  207.                            set(App->Div_Erg, MUIA_Text_Contents, x);
  208.                         }
  209.                       auswertung(richtig,falsch,&prozg,&prozm,&prozd,&proza,&prozs,&gesr,&gesf);
  210.                       inttostr(prozm,x);
  211.                       set(App->Gauge_Mul, MUIA_Gauge_Current, prozm);
  212.                       set(App->Gauge_Div, MUIA_Gauge_Current, prozd);
  213.                       set(App->Gauge_Addi, MUIA_Gauge_Current, proza);
  214.                       set(App->Gauge_Subtr, MUIA_Gauge_Current, prozs);
  215.                       set(App->Gauge_Ges, MUIA_Gauge_Current, prozg);
  216.                       inttostr(gesr,x);
  217.                       set(App->Gesammt_Richtig, MUIA_Text_Contents, x);
  218.                       inttostr(gesf,x);
  219.                       set(App->Gesammt_Falsch, MUIA_Text_Contents, x);
  220.                       --i;
  221.                       if(i)
  222.                         {
  223.                            set(App->Erg_Div, MUIA_String_Contents, "");
  224.                            inttostr(j,z);
  225.                            set(App->Div_Nr, MUIA_Text_Contents, z);
  226.                            erg=Division(str);
  227.                            set(App->Aufg_Div, MUIA_Text_Contents, str);
  228.                         }
  229.                       else
  230.                         {
  231.                            set(App->Aufg_Div, MUIA_Text_Contents, GetMBString(MSG_Ready));
  232.                            set(App->Erg_Div, MUIA_Text_Contents, "");
  233.                         }
  234.                      }
  235.                     break;
  236.                     case ANZ_ADDI:
  237.                     get(App->Anz_Addi, MUIA_String_Contents, &mal);
  238.                     
  239.                     if (atoi(mal) != 0 )
  240.                        {
  241.                         erg=Addition(str);
  242.                         set(App->Aufg_Addi, MUIA_Text_Contents, str);
  243.                         set(App->Addi_Nr, MUIA_Text_Contents, "1");
  244.                         i=atoi(mal);
  245.                         j=1;
  246.                        }
  247.                     else
  248.                        {
  249.                         set(App->Aufg_Addi, MUIA_Text_Contents,GetMBString(MSG_Rubish));
  250.                         set(App->Addi_Erg_tex, MUIA_Text_Contents,GetMBString(MSG_Rubish));
  251.                        }
  252.                     break;
  253.                 case ERG_ADDI:
  254.                     get(App->Erg_Addi, MUIA_String_Contents, &erga);
  255.                     set(App->Addi_Erg, MUIA_Text_Contents, "");
  256.                     if(i)
  257.                      {
  258.                       ++j;
  259.                       if (erg == atoi(erga))
  260.                         {
  261.                            set(App->Addi_Erg_tex, MUIA_Text_Contents,GetMBString(MSG_Right));
  262.                            ++richtig[2];
  263.                            inttostr(richtig[2],x);
  264.                            set(App->Addi_Richtig, MUIA_Text_Contents, x);
  265.                         }
  266.                       else
  267.                         {
  268.                            ++falsch[2];
  269.                            inttostr(falsch[2],x);
  270.                            set(App->Addi_Falsch, MUIA_Text_Contents, x);
  271.                            set(App->Addi_Erg_tex, MUIA_Text_Contents,GetMBString(MSG_Wrong));
  272.                            inttostr(erg,x);
  273.                            set(App->Addi_Erg, MUIA_Text_Contents, x);
  274.                         }
  275.                       auswertung(richtig,falsch,&prozg,&prozm,&prozd,&proza,&prozs,&gesr,&gesf);
  276.                       inttostr(prozm,x);
  277.                       set(App->Gauge_Mul, MUIA_Gauge_Current, prozm);
  278.                       set(App->Gauge_Div, MUIA_Gauge_Current, prozd);
  279.                       set(App->Gauge_Addi, MUIA_Gauge_Current, proza);
  280.                       set(App->Gauge_Subtr, MUIA_Gauge_Current, prozs);
  281.                       set(App->Gauge_Ges, MUIA_Gauge_Current, prozg);
  282.                       inttostr(gesr,x);
  283.                       set(App->Gesammt_Richtig, MUIA_Text_Contents, x);
  284.                       inttostr(gesf,x);
  285.                       set(App->Gesammt_Falsch, MUIA_Text_Contents, x);
  286.                       --i;
  287.                       if(i)
  288.                         {
  289.                            set(App->Erg_Addi, MUIA_String_Contents, "");
  290.                            inttostr(j,z);
  291.                            set(App->Addi_Nr, MUIA_Text_Contents, z);
  292.                            erg=Addition(str);
  293.                            set(App->Aufg_Addi, MUIA_Text_Contents, str);
  294.                         }
  295.                       else
  296.                         {
  297.                            set(App->Aufg_Addi, MUIA_Text_Contents,GetMBString(MSG_Ready));
  298.                            set(App->Erg_Addi, MUIA_Text_Contents, "");
  299.                         }
  300.                      }
  301.                     break;
  302.                     case ANZ_SUBTR:
  303.                     get(App->Anz_Subtr, MUIA_String_Contents, &mal);
  304.                     
  305.                     if (atoi(mal) != 0 )
  306.                        {
  307.                         erg=Subtraktion(str);
  308.                         set(App->Aufg_Subtr, MUIA_Text_Contents, str);
  309.                         set(App->Subtr_Nr, MUIA_Text_Contents, "1");
  310.                         i=atoi(mal);
  311.                         j=1;
  312.                        }
  313.                     else
  314.                        {
  315.                         set(App->Aufg_Subtr, MUIA_Text_Contents,GetMBString(MSG_Rubish));
  316.                         set(App->Subtr_Erg_tex, MUIA_Text_Contents,GetMBString(MSG_Rubish));
  317.                        }
  318.                     break;                                      
  319.                 case ERG_SUBTR:
  320.                     get(App->Erg_Subtr, MUIA_String_Contents, &erga);
  321.                     set(App->Subtr_Erg, MUIA_Text_Contents, "");
  322.                     if(i)
  323.                      {
  324.                       ++j;
  325.                       if (erg == atoi(erga))
  326.                         {
  327.                            set(App->Subtr_Erg_tex, MUIA_Text_Contents,GetMBString(MSG_Right));
  328.                            ++richtig[3];
  329.                            inttostr(richtig[3],x);
  330.                            set(App->Subtr_Richtig, MUIA_Text_Contents, x);
  331.                         }
  332.                       else
  333.                         {
  334.                            ++falsch[3];
  335.                            inttostr(falsch[3],x);
  336.                            set(App->Subtr_Falsch, MUIA_Text_Contents, x);
  337.                            set(App->Subtr_Erg_tex, MUIA_Text_Contents,GetMBString(MSG_Wrong));
  338.                            inttostr(erg,x);
  339.                            set(App->Subtr_Erg, MUIA_Text_Contents, x);
  340.                         }
  341.                       auswertung(richtig,falsch,&prozg,&prozm,&prozd,&proza,&prozs,&gesr,&gesf);
  342.                       inttostr(prozm,x);
  343.                       set(App->Gauge_Mul, MUIA_Gauge_Current, prozm);
  344.                       set(App->Gauge_Div, MUIA_Gauge_Current, prozd);
  345.                       set(App->Gauge_Addi, MUIA_Gauge_Current, proza);
  346.                       set(App->Gauge_Subtr, MUIA_Gauge_Current, prozs);
  347.                       set(App->Gauge_Ges, MUIA_Gauge_Current, prozg);
  348.                       inttostr(gesr,x);
  349.                       set(App->Gesammt_Richtig, MUIA_Text_Contents, x);
  350.                       inttostr(gesf,x);
  351.                       set(App->Gesammt_Falsch, MUIA_Text_Contents, x);
  352.                       --i;
  353.                       if(i)
  354.                         {
  355.                            set(App->Erg_Subtr, MUIA_String_Contents, "");
  356.                            inttostr(j,z);
  357.                            set(App->Subtr_Nr, MUIA_Text_Contents, z);
  358.                            erg=Subtraktion(str);
  359.                            set(App->Aufg_Subtr, MUIA_Text_Contents, str);
  360.                         }
  361.                       else
  362.                         {
  363.                            set(App->Aufg_Subtr, MUIA_Text_Contents,GetMBString(MSG_Ready));
  364.                            set(App->Erg_Subtr, MUIA_Text_Contents, "");
  365.                         }
  366.                      }
  367.                     break;
  368.                 case LADEN:     Dateilesen(richtig,falsch);
  369.                     break;
  370.                 case SPEICHERN: Dateischreiben(richtig,falsch);
  371.                     break;
  372.                 }
  373.         if (running && signal) Wait(signal);
  374.         }
  375.         DisposeApp(App);
  376.         end();
  377. }
  378.  
  379. void auswertung(int ri[5], int fa[5], int *prozg, int *prom, int *prod, int *proa, int *pros, int *ria, int *faa )
  380. {
  381.     int pr, i;
  382.     *ria=0;
  383.     *faa=0;
  384.     for (i=0;i<4;++i)
  385.         {
  386.            *ria=*ria+ri[i];
  387.            *faa=*faa+fa[i];
  388.         }
  389.      pr=(*ria)+(*faa);
  390.      if(pr)
  391.      {
  392.       *prozg=(int)((float(*ria)/float(pr))*100);
  393.       //Mul
  394.       if(ri[0]||fa[0])
  395.        *prom=(int)((float(ri[0])/(float(ri[0])+float(fa[0])))*100);
  396.       //Div
  397.       if(ri[1]||fa[1])
  398.        *prod=(int)((float(ri[1])/(float(ri[1])+float(fa[1])))*100);
  399.       //Addi
  400.       if(ri[2]||fa[2])
  401.        *proa=(int)((float(ri[2])/(float(ri[2])+float(fa[2])))*100);
  402.       //Subtr
  403.       if(ri[3]||fa[3])
  404.        *pros=(int)((float(ri[3])/(float(ri[3])+float(fa[3])))*100);
  405.       }
  406.      else
  407.        *prozg=0
  408. }
  409. int Multiplikation(char str[12])
  410. {
  411.        int f1,f2,erg,g1=50,g2=50;
  412.        char f11[6],f22[6],f33[12]="";
  413.                     do
  414.                      {
  415.                         Zufall(&f1,&f2,g1,g2);
  416.                         erg=f1*f2;
  417.                      }
  418.                     while(erg>100||f1==0||f2==0);
  419.                     inttostr(f1,f11);
  420.                     inttostr(f2,f22);
  421.                     strcat(f33,f11);
  422.                     strcat(f33," * ");
  423.                     strcat(f33,f22);
  424.                     strcpy(str,f33);
  425.                     return(erg);
  426. }
  427. int Division(char str[12])
  428. {
  429.        int f1,f2,erg,g1=800,g2=50,tmp;
  430.        char f11[6],f22[6],f33[12]="";
  431.                 do
  432.                  {
  433.                    Zufall(&f1,&f2,g1,g2);
  434.                    tmp=f1/f2;
  435.                    f1=tmp*f2;
  436.                    erg=f1/f2;
  437.                  }
  438.                 while (erg<=1||f1<=f2||erg>100||f2==0);
  439.                 inttostr(f1,f11);
  440.                 inttostr(f2,f22);
  441.                 strcat(f33,f11);
  442.                 strcat(f33," : ");
  443.                 strcat(f33,f22);
  444.                 strcpy(str,f33);
  445.                 return(erg);
  446. }
  447. int Addition(char str[12])
  448. {
  449.        int f1,f2,erg,g1=50,g2=50;
  450.        char f11[6],f22[6],f33[6]="";
  451.                     do
  452.                      {
  453.                         Zufall(&f1,&f2,g1,g2);
  454.                         erg=f1+f2;
  455.                      }
  456.                     while(erg>100);
  457.                     inttostr(f1,f11);
  458.                     inttostr(f2,f22);
  459.                     strcat(f33,f11);
  460.                     strcat(f33," + ");
  461.                     strcat(f33,f22);
  462.                     strcpy(str,f33);
  463.                     return(erg);
  464. }
  465. int Subtraktion(char str[12])
  466. {
  467.        int f1,f2,erg,g1=150,g2=150;
  468.        char f11[6],f22[6],f33[12]="";
  469.                     do
  470.                      {
  471.                         Zufall(&f1,&f2,g1,g2);
  472.                         erg=f1-f2;
  473.                      }
  474.                     while (erg>100||f1<f2||erg<=0);
  475.                     inttostr(f1,f11);
  476.                     inttostr(f2,f22);
  477.                     strcat(f33,f11);
  478.                     strcat(f33," - ");
  479.                     strcat(f33,f22);
  480.                     strcpy(str,f33);
  481.                     return(erg);
  482. }
  483. /* ----------------------------------- init ------------------------------------
  484.  
  485.  Kommentar: Zum öffnen der benötigten Libraries und des Katalogs
  486.  
  487. */
  488. void init(void)
  489. {
  490.         if (!(MUIMasterBase = OpenLibrary(MUIMASTER_NAME,MUIMASTER_VMIN)))
  491.         {
  492.                 printf("Can't Open MUIMaster Library\n");
  493.                 exit(20);
  494.         }
  495.         if (!(LocaleBase = OpenLibrary("locale.library",38)))
  496.         {
  497.                 printf("Can't Open Locale Library\n");
  498.                 printf("Built-in Language will be used !!!\n");
  499.         }
  500.         OpenAppCatalog(NULL,NULL);
  501. }
  502.  
  503. /* ------------------------------------ end ------------------------------------
  504.  
  505.  Kommentar: Ende zum schließen der geöffneten Libraries und des Katalogs
  506.  
  507. */
  508. void end(void)
  509. {
  510.         CloseAppCatalog();
  511.         if (LocaleBase)
  512.                 CloseLibrary(LocaleBase);
  513.         CloseLibrary(MUIMasterBase);
  514.         exit(0);
  515. }
  516.  
  517. void Zufall(int *z1, int *z2, int gr1, int gr2)
  518. {
  519.         do
  520.          {
  521.             *z1=Random(gr1);
  522.             *z2=Random(gr2);
  523.          }
  524.         while(*z1<=1||*z2<=1);
  525.         return;
  526. }
  527.  
  528.